import {util,get} from '../../../utils/util'
import { WeElement, define } from 'omi'
import './index.css'
import logo from './jingqu.png'
import { list } from 'postcss'

import api from '../../../api/api.js'

define('page-logs', class extends WeElement {
  config = {
    navigationBarTitleText: '景区门票'
  }

  data = {
    list: [{},{}],
    scenic_list:[],
    latitude:"",
    longitude:"",
    distance:""
  }

  install(option) {
    console.log(option)
   //获取景区列表
    get(api.get_scenic_list).then((res)=>{
      this.data.scenic_list=res.data;
      console.log(this.data.scenic_list);
      this.update()
    })

      //获取用户当前位置
      wx.getLocation({
        type: 'wgs84',
        //获取成功
        success: (res) => {
          console.log(res);
          this.data.latitude = res.latitude
          this.data.longitude = res.longitude
          console.log("latitude", this.data.latitude);
          console.log("longitude", this.data.longitude);
          this.distance(this.data.latitude, this.data.longitude, 29.2491900000, 115.5012880000)
          this.update()
        },
        //获取失败
        fail: (res) => {
          console.log(res);
          //设置位置授权
          wx.getSetting({
            success: (res) => {
              console.log("successSet", res)
              var statu = res.authSetting;
              //提示位置授权提醒
              if (!statu['scope.userLocation']) {
                wx.showModal({
                  title: '是否授权当前位置',
                  content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                  success: (tip) => {
                    console.log("tip", tip);
                    //确定位置授权
                    if (tip.confirm) {
                      console.log("tip.confirm",tip.confirm);
                      //手动授权
                      wx.openSetting({
                        success: (open_res) => {
                          console.log("open_res", open_res)
                          if (open_res.authSetting["scope.userLocation"] === true) {
                            wx.showToast({
                              title: '授权成功',
                              icon: 'success',
                              duration: 1000
                            })
                            //授权成功后，获取用户当前位置
                            wx.getLocation({
                              success: (res) => {
                                console.log("授权成功后的位置", res);
                                this.data.latitude = res.latitude
                                this.data.longitude = res.longitude
                                console.log("latitude", this.data.latitude);
                                console.log("longitude", this.data.latitude);
                                this.distance(this.data.latitude, this.data.longitude, 29.2491900000, 115.5012880000)
                                this.update()
                              },
                            })
                          } else {
                            console.log("授权失败");
                            wx.showToast({
                              title: '授权失败',
                              duration: 1000
                            })
                          }
                        }
                      })
                    } 
                    else {//取消位置授权
                      console.log("cancel授权失败");
                      console.log(this.data.distance)
                    }
                  }
                })
              } else {

              }
            }
          })
        }
      })

    this.update()
  }

//计算用户当前位置与商家位置之间的距离
  distance = (la1, lo1, la2, lo2) => {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137;//地球半径
    s = Math.round(s * 10000) / 10000;
    console.log("计算结果", s)
    this.data.distance = s.toFixed(2)
    console.log(this.data.distance)
    this.update()
  }

  counterChange = (evt) => {
    console.log(evt.detail)
  }
//跳转细节页
link=(e)=>{
  console.log(e);
  wx.navigateTo({
    url:"../inner/index?scenic_id="+e.currentTarget.dataset.id
  })
}

  render() {
    return (
      <view class="web">
        <view class="list_box">
          {scenic_list.map( (item,index)=>(
           
            <view class="marg" bindtap={this.link} data-id={item.scenic_id} key={index}>
                <view class="img_box">
                  <image src={item.full_diagram}></image>
                </view>
                <view class="text_box">
                  <text class="title_jq">{item.scenic_name}</text>
                  {/* <text class="xiangq">详情</text> */}
                  {
                  distance == "" ? <text >位置无法获取</text>:<text >距您位置{distance}km</text>
                  }
                  <text class="bottom_text">已售{item.sale_num}</text>
                </view>
                
                  <view class="money">
                    <text class="money_text">¥</text>
                    <text class="money_num">{item.price}</text>
                    <text class="money_text">起</text>
                  </view>
              </view>
         
          )
        )}
        </view>
        
        {/* <view class="butm_text">
          <text>我是有底线的</text>
        </view> */}
      </view>
    )
  }
})